package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetMealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {

    //业务层对象
    @Reference
    private SetMealService setMealService;

    //Redis连接池
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/upload.do")
    public Result upload(@RequestParam("imgFile") MultipartFile multipartFile) {
        Jedis resource = null;
        try {
            resource = jedisPool.getResource();       //获取jedis池的资源
            String originalFilename = multipartFile.getOriginalFilename(); //获取源文件名
            String extension = originalFilename.substring(originalFilename.lastIndexOf(".") - 1);   //获取文件扩展名{
            String fileName = UUID.randomUUID().toString() + extension;     //生成文件名
            //上传文件到七牛云
            QiniuUtils.upload2Qiniu(multipartFile.getBytes(), fileName);
            //将有的文件数据保存到redis 这里只是上传完成 不存为数据库的资源
            resource.sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }finally {
            //释放资源
            if(resource != null){
                resource.close();
            }
        }
    }

    //保存数据
    @RequestMapping("/add.do")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try{
            setMealService.add(setmeal,checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_SETMEAL_FAIL);
        }

        return new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    //分页查询
    @RequestMapping("/findPage.do")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return setMealService.findPage(queryPageBean);
    }


    //通过setmealId获取checkgroupIds
    @RequestMapping("/findCheckgroupIds.do")
    public Result findCheckgroupIds(Integer setmealId) {
        try {
            List<Integer> checkgroupIds = setMealService.findCheckgroupIds(setmealId);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkgroupIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    //重新生成静态页面
    @RequestMapping("/generateStaticHtml.do")
    public Result generateStaticHtml(){
        try{
            setMealService.generateMobileStaticHtml();
            return new Result(true,MessageConstant.GENERATE_HTML_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.GENERATE_HTML_FAIL);
        }
    }

    //保存编辑
    @RequestMapping("/edit.do")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        try{
            setMealService.edit(setmeal,checkgroupIds);
            return new Result(true,MessageConstant.EDIT_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }

    //删除套餐
    @RequestMapping("/deleteById.do")
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    public Result deleteById(Integer id, String img){
        Jedis resource = null;
        try{
            resource = jedisPool.getResource();       //获取jedis池的资源
            setMealService.deleteById(id);
            if(img != null) {
                //从七牛云删除图片
                QiniuUtils.deleteFileFromQiniu(img);
                //把图片从redis集合中删除
                //resource.srem(RedisConstant.SETMEAL_PIC_RESOURCES,img);
            }
            return new Result(true,MessageConstant.DELETE_SETMEAL_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_SETMEAL_FAIL);
        }finally {
            if(resource != null){
                resource.close();
            }
        }
    }

}
